﻿
/// <summary>
/// 解压文件
/// </summary>

using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;

using WCFMINERP.SharpZipLib.BZip2;
using WCFMINERP.SharpZipLib.Zip;
using WCFMINERP.SharpZipLib.Zip.Compression;
using WCFMINERP.SharpZipLib.Zip.Compression.Streams;
using WCFMINERP.SharpZipLib.GZip;

namespace WCFMINERP.SharpZipLib
{
    public class UnZipClass
    {
        public void UnZip(string[] args)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));

            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                
                //string directoryName = Path.GetDirectoryName(args[1]);
                string fileName = Path.GetFileName(theEntry.Name);

                ////生成解压目录
                //Directory.CreateDirectory(directoryName);

                if (fileName != String.Empty)
                {
                    //解压文件到指定的目录
                    FileStream streamWriter = File.Create(args[1] +"\\" +fileName);

                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }

                    streamWriter.Close();
                }
            }
            s.Close();
        }

        public static void UnZip(string sourceFilePath,string desFilePath)
        {
            if (!Directory.Exists(desFilePath))
            {
                Directory.CreateDirectory(desFilePath);
            }
            ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFilePath));

            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {

                //string directoryName = Path.GetDirectoryName(args[1]);
                string fileName = Path.GetFileName(theEntry.Name);

                ////生成解压目录
                //Directory.CreateDirectory(directoryName);

                if (fileName != String.Empty)
                {
                    //解压文件到指定的目录
                    FileStream streamWriter = File.Create(desFilePath + "\\" + fileName);

                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }

                    streamWriter.Close();
                }
            }
            s.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inFromFileName">从这里解压</param>
        /// <param name="tarDir">解压到这里</param>
        public static void ExtractZip(string inFromFileName,string tarDir)
        {
            FastZip fz = new FastZip();
            fz.ExtractZip(inFromFileName, tarDir, "");
            fz = null;
        }
    }
}
